//
//  LXCarStockViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2022/7/8.
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXCarStockViewController.h"
#import "LXCommSearchView.h"
#import "LXCarStockListViewController.h"
#import "LXCarLeaseApplyViewController.h"
#import "LXCarSellApplyViewController.h"
@interface LXCarStockViewController ()<UIScrollViewDelegate>
@property (nonatomic) UIScrollView *scrollView;
@property (nonatomic) UIButton *topBtn;
@property (nonatomic) UIImageView *topImgV;
@property (nonatomic) UIView *topView;
@property (nonatomic) NSInteger selIndex;
@property (nonatomic) NSMutableArray *titlesAry;
@end
#define lineWidth 17
@implementation LXCarStockViewController


-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self setCountData];
    
    if (kUserDefaultForBool(kRefreshStr)) {
        kUserDefaultSetBool(NO, kRefreshStr);
        [kNotifyCenter postNotificationName:@"reloadStockData" object:nil];
    }
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self layoutCustomNav:self.view title:@"车型库存"];
    
    self.titlesAry = [NSMutableArray array];
    
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addBtn.frame = CGRectMake(ScreenWidth-35, KStatuesBarHeight + 9, 20, 20);
    [addBtn setImage:Image(@"addcarBtn") forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(clickAddAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addBtn];
    
    
    
    LXCommSearchView *searchaView = [LXCommSearchView shareInstance];
    searchaView.frame = CGRectMake(0, kViewTopHeight, kScreenWidth, 60);
    [searchaView.searchBtn setTitle:@"请输入品牌/型号进行搜索" forState:UIControlStateNormal];
    [self.view addSubview:searchaView];
    [searchaView.searchBtn whenTapped:^{
        LXCarStockListViewController *vc = [[LXCarStockListViewController alloc] init];
        vc.type = self.selIndex + 1;
        vc.isSearch = YES;
        [self pushToNextController:vc];
        
    }];
    
    
    UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, searchaView.Y_height + 5, ScreenWidth, 30)];
    self.topView = topView;
    [self.view addSubview:topView];
    
    
    NSArray *ary = @[@"出租车型(0)",@"出售车型(0)"];
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, topView.Y_height, self.view.width, ScreenHeight - topView.Y_height)];
    scrollView.backgroundColor = [UIColor clearColor];
    scrollView.contentSize = CGSizeMake(ScreenWidth * ary.count, 0);
    scrollView.pagingEnabled = YES;
    scrollView.bounces = NO;
    scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    scrollView.delegate = self;
    self.scrollView = scrollView;
    [self.view addSubview:scrollView];
    
    
    CGFloat X = 15;
    for (int i = 0; i < ary.count; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(X, 0, 120, 20)];
        [btn setTitle:ary[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithHexString:@"#AAAAAA"] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithHexString:@"#333333"] forState:UIControlStateSelected];
        btn.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        btn.tag = i + 100;
        [btn addTarget:self action:@selector(clickBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [topView addSubview:btn];
        if (i == 0) {
            btn.selected = YES;
            self.selIndex = i;
            self.topBtn = btn;
            self.topImgV = [[UIImageView alloc] initWithFrame:CGRectMake(X + (btn.width - lineWidth)/2, topView.height-8, lineWidth, 6)];
            self.topImgV.image = kImg(@"分段下标");
            [topView addSubview:self.topImgV];
        }
        [self.titlesAry addObject:btn];
        X = CGRectGetMaxX(btn.frame)+30;
        
        
        LXCarStockListViewController *vc = [[LXCarStockListViewController alloc] init];
        vc.view.frame = CGRectMake(i * ScreenWidth, 0, ScreenWidth, scrollView.height);
        vc.type = i + 1;
        [scrollView addSubview:vc.view];
        [self addChildViewController:vc];
    }
    
    
    
}
-(void)setCountData{
    NSString *url = [DOMAIN_URL stringByAppendingFormat:GET_GetCatTypeListCount];
    [MSNetworkManager getRevertUrl:url params:@{} success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        if (isSuccess) {
       
            // 租车数量
            NSString *rentNum = [NSString stringWithFormat:@"%@",resonseObj[@"rentNum"]];
            // 售车数量
            NSString *saleNum = [NSString stringWithFormat:@"%@",resonseObj[@"saleNum"]];
           
            
            NSArray *ary = @[[NSString stringWithFormat:@"出租车型(%@)",rentNum],[NSString stringWithFormat:@"出售车型(%@)",saleNum]];
            
            for (int i = 0; i < self.titlesAry.count; i++) {
                UIButton *btn = self.titlesAry[i];
                [btn setTitle:ary[i] forState:UIControlStateNormal];
            }
        }
        
    
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:NO];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGPoint p = scrollView.contentOffset;
    float w = p.x;
    int index = w  /scrollView.frame.size.width;
    
    self.topBtn.selected = NO;
    self.selIndex = index;
    UIButton *btn = [self.topView viewWithTag:index + 100];
    self.topImgV.X = btn.X + (btn.width - lineWidth)/2;
    btn.selected = YES;
    self.topBtn = btn;
    
}
#pragma mark -- 点击topView按钮
-(void)clickBtnAction:(UIButton *)sender{
    if (sender.isSelected) return;
    sender.selected = YES;
    self.topBtn.selected = NO;
    self.topImgV.X = sender.X + (sender.width - lineWidth)/2;
    self.topBtn = sender;
    NSInteger pageCount = sender.tag - 100;
    self.selIndex = pageCount;
    [self.scrollView setContentOffset:CGPointMake(pageCount * self.scrollView.bounds.size.width, 0)];


   
}
-(void)clickAddAction{
    
    
    // 新增发布
    if (self.selIndex == 0) {
        LXCarLeaseApplyViewController *vc = [[LXCarLeaseApplyViewController alloc] init];
        vc.type = 1;
        [self pushToNextController:vc];
    }
    else {
        
        LXCarSellApplyViewController *vc = [[LXCarSellApplyViewController alloc] init];
        vc.type = 1;
        [self pushToNextController:vc];
    }
    
}
@end
